<template>
	<view class="content">
		<view class="logoBox">
			<image :src="imgUrl+'/static/wap/img/dian@2x.png'" mode="" class="logo"></image>
		</view>
		<view class="name">能源运维</view>
		<view class="bg">
			   <!-- #ifdef MP-WEIXIN -->
			              <button @click="wxloginxCx" class='LoginBox' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxlogin" plain="true" style="border:0">
			                   <view class="loginTxt">微信绑定</view>
			              </button>
			    <!-- #endif -->
				<button></button>
			   <!-- #ifdef H5 -->
			              <button @click="wxloginH5" class='LoginBox' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxlogin" plain="true" style="border:0">
			                   <view class="loginTxt">微信绑定</view>
			              </button>
			    <!-- #endif -->
				<button @click="goback" class='LoginBox LoginBox2' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxlogin" plain="true" style="border:0">
				     <view class="loginTxt">取消</view>
				</button>
			<image :src="imgUrl+'/static/wap/img/spbg@2x.png'" mode="" class="bgimg"></image>
		</view>
	</view>
	
</template>

<script>
	import util from '../../static/util.js'
	export default {
		data() {
			return {
				imgUrl:util.globalData.imgPre,
				title: 'Hello',
				code:'',
			}
		},
		// onBackPress(e) {  
		// 				// 这里可以自定义返回逻辑，比如下面跳转其他页面
		// 				console.log('监听返回按钮')
		// 						// return true 表示禁止默认返回
								
		// 						uni.switchTab({
		// 							url:'../index/index'
		// 						})
		// 					return true
		// 					}, 
		onLoad() {
			// https://nengyuan.chuangxiangdianli.com/h5/#/

			//var url='https://nengyuan.chuangxiangdianli.com/h5/?code=001hqzXm0pAein1OzzWm0ZSyPn2hqzXD&state=STATE#/pages/login/loginWx'
			this.code=this.getQueryString('code');
			var ua = window.navigator.userAgent.toLowerCase();
			if (ua.match(/micromessenger/i) == 'micromessenger') {
			    // console.log('是微信客户端')
			   let url = window.location.href.split('#')[0]
			   let serch = url.split('?')[1]
			   let _this = this;
			   if (serch) {
			   	let arr = serch.split('&')
			   	let code = arr[0].split('code=')[1]
			   	// 此时获取到的code码需要你传给后端，因为微信的安全机制，前端不能做处理
			   } else {
			   	return null
			   }
			} else {
			    // console.log('不是微信客户端')
			   // return false;
			}
           
		},
		onShow(){
			//var url='https://nengyuan.chuangxiangdianli.com/h5/?code=001hqzXm0pAein1OzzWm0ZSyPn2hqzXD&state=STATE#/pages/login/loginWx'
			//var url='https://nengyuan.chuangxiangdianli.com/h5/#/pages/login/loginWx'
			this.code=this.getQueryString('code');
			var ua = window.navigator.userAgent.toLowerCase();
			if (ua.match(/micromessenger/i) == 'micromessenger') {
			    // console.log('是微信客户端')
			   let url = window.location.href.split('#')[0]
			   let serch = url.split('?')[1]
			   let _this = this;
			   if (serch) {
			   	let arr = serch.split('&')
			   	let code = arr[0].split('code=')[1]
			   	// 此时获取到的code码需要你传给后端，因为微信的安全机制，前端不能做处理 
				this.wxloginhttpH5()
			   } else {
			   	return null
			   }
			} else {
			   // return false;
			}
			
		},
		methods: {
			wxlogin(){ 
			},
			goback(){
				uni.switchTab({
					url:"../index/index"
				})
			},
			getQueryString (name) {
			  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
			  var r = window.location.search.substr(1).match(reg)
			  if (r != null) return unescape(r[2]); return null
			},
			wxloginhttpH5(){
				var that=this;
				that.ajax({
					 url:'api/user/publicLogin',
					 data:{
						 code:that.code,
					 },
					success:function(res){
						 if(res.code==200){
							 uni.navigateTo({
							 	url:'login?wechat_id='+res.data.wechat_id+'&type=2'
							 }) 
						 }else{
							 uni.showToast({
							 	title:"" + res.msg,
							 	icon:'none'
							 });
						 }
						
					}
				})
			},
			wxloginH5(){ 
				var code=this.getQueryString('code');
				if(code!=''&&code!=null){
					// console.log("不为空")
				}else{
					let local = encodeURIComponent(window.location.href)  
					var str = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaa58ad335cd9ec2a&redirect_uri='+local+'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
					console.log(str)
					return
					window.location.href = str
				}
				
			},
			
           wxloginxCx(){
			   var that=this;
			   uni.getProvider({
			       service: 'oauth',
			       success: function (res) { 
					   console.log(res) 
			   		 if (1) {   
			               uni.login({
			                   provider: 'weixin',
			                   success: function (loginRes) {  
			   					 var data=JSON.stringify(loginRes);
								 var loginRes=loginRes
			   					// 获取用户信息+0
			   					 uni.getUserInfo({
			   					     provider: 'weixin',
			   					     success: function (infoRes) {   
										 that.ajax({
											 url:'api/user/appletLogin',
											 data:{
												 code:loginRes.code,
												 encryptedData:infoRes.encryptedData,
												 iv:infoRes.iv,
												 signature:infoRes.signature,
												 rawData:infoRes.rawData,
											 },
											success:function(res){ 
												if(res.code==200){
														uni.setStorage({
														    key: 'openId',
														    data: res.data.openId,
														    success: function () {
																uni.navigateTo({
																	url:'login?wechat_id='+res.data.wechat_id+'&type=1'
																})
														    }
														});

												}else{ 
													 uni.showToast({
														title:"" + res.msg,
														icon:'none'
													 });
												}
												
											} 
										 }) 
			   					     }  
			   				    });
			                   },
								fail:function(err){
									console.log(err)
								}
			               });	
			           }else{
			   			console.log('微信登录else')
			   		}
			       },
				   fail(err) {
					console.log(err)
				   }
			   });
		   }
		}
	}
</script>

<style lang="scss" scoped>

	.content{
		height: 100vh;
	}
	.logo{
		width: 200rpx;
		height: 200rpx;
	}
	.logoBox{
		text-align: center;
		padding-top: 200rpx;
	}
	.name{
		font-size: 63rpx;
		font-family: FZHuaLi-M14S Regular, FZHuaLi-M14S Regular-Regular;
		font-weight: 400;
		text-align: center;
		color: #87888b;
		padding-top: 116rpx;
	}
	.bg{
		width: 100%;
		height: 421rpx;
		position: fixed;
		left: 0;
		bottom: 30rpx;
	}
	.bgimg{
		width: 100%;
	}
	.LoginBox{
		width: 430rpx;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 36rpx;
		color: #FFFFFF;
		background: #209698;
		border-radius: 45rpx;
		position: absolute;
		left: 160rpx;
		top: -45rpx;
		z-index: 1111111;
	}
	.loginTxt{
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 36rpx;
		color: #FFFFFF;
		background: #209698;
		border-radius: 45rpx;
	}
	.LoginBox2{
		top: 80rpx;
	}
</style>
